<?php
class Payments extends CI_Model
{	
	/*
	Determines if a given person_id is a payment
	*/
	function exists($payment_id)
	{
		$this->db->from('payment');	
		$this->db->where('payment.payment_id',$payment_id);
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}
	
	/*
	Returns all the payment
	*/
	function get_all($limit=10000, $offset=0)
	{
		$this->db->from('payment');
		$this->db->limit($limit);
		$this->db->offset($offset);
		return $this->db->get();		
	}
	
//	function get_list_cus($limit=10000, $offset=0)
//	{
//		$this->db->from('payment');
//		$this->db->join('people','payment.person_id=people.person_id');			
//		$this->db->where('deleted',0);
//		$this->db->order_by("last_name", "asc");
//		$this->db->limit($limit);
//		$this->db->offset($offset);
//		$data = array();
//        $Q = $this->db->get();
//        if ($Q->num_rows() > 0){
//	       	foreach ($Q->result_array() as $row){
//	         	$data[] = $row;
//	       	}
//        }
//	    $Q->free_result(); 
//	    
//	    return $data; 
//		
//	}
//	
	function count_all()
	{
		$this->db->from('payment');
		return $this->db->count_all_results();
	}
//	
//	/*
//	Gets information about a particular payment
//	*/
	function get_info($payment_id)
	{
		$this->db->from('payment');	
		$this->db->where('payment.payment_id',$payment_id);
		$query = $this->db->get();
		
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//Get empty base parent object, as $giftcard_id is NOT an giftcard
			$payment_obj=new stdClass();

			//Get all the fields from giftcards table
			$fields = $this->db->list_fields('payment');

			foreach ($fields as $field)
			{
				$payment_obj->$field='';
			}

			return $payment_obj;
		}
	}
//	
//	/*
//	Gets information about multiple payment
//	*/
//	function get_multiple_info($customer_ids)
//	{
//		$this->db->from('payment');
//		$this->db->join('people', 'people.person_id = payment.person_id');		
//		$this->db->where_in('payment.person_id',$customer_ids);
//		$this->db->order_by("last_name", "asc");
//		return $this->db->get();		
//	}
//	
//	/*
//	Inserts or updates a payment
//	*/
	function save(&$payment_data,$payment_id=false)
	{
		$success= false;
		
		if(!$payment_id or !$this->exists($payment_id) )
		{
                    if($this->db->insert('payment',$payment_data))
                    {
                            $payment_data['payment_id']=$this->db->insert_id();
                            return true;
                    }
                    return false;
		}
		
		$this->db->where('payment_id', $payment_id);
		return $this->db->update('payment',$payment_data);
	}
//	
//	/*
//	Deletes one payment
//	*/
        
	function delete($payment_id)
	{
		$this->db->where('payment_id', $payment_id);
		$this->db->delete('payment');
                return true;
	}
//	
//	/*
//	Deletes a list of payment
//	*/
	function delete_list($payment_ids)
	{
		$this->db->where_in('payment_id',$payment_ids);
		$this->db->delete('payment');
                return true;
 	}
// 	
// 	/*
//	Get search suggestions to find payment
//	*/
	function get_search_suggestions($search,$limit=25)
	{
		$suggestions = array();
		
		$this->db->from('payment');
		$this->db->where("(name LIKE '%".$this->db->escape_like_str($search)."%' or 
		last_name LIKE '%".$this->db->escape_like_str($search)."%')");
		$this->db->order_by("name", "asc");		
		$by_name = $this->db->get();
		foreach($by_name->result() as $row)
		{
			$suggestions[]=array('label'=> $row->name);		
		}
		
		
		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;
	
	}
//	
//	/*
//	Get search suggestions to find payment
//	*/
//	function get_customer_search_suggestions($search,$limit=25)
//	{
//		$suggestions = array();
//		
//		$this->db->from('payment');
//		$this->db->join('people','payment.person_id=people.person_id');	
//		$this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or 
//		last_name LIKE '%".$this->db->escape_like_str($search)."%' or 
//		CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");
//		$this->db->order_by("last_name", "asc");		
//		$by_name = $this->db->get();
//		foreach($by_name->result() as $row)
//		{
//			$suggestions[]=array('value'=> $row->person_id, 'label' => $row->first_name.' '.$row->last_name);		
//		}
//		
//		$this->db->from('payment');
//		$this->db->join('people','payment.person_id=people.person_id');	
//		$this->db->where('deleted',0);		
//		$this->db->like("account_number",$search);
//		$this->db->order_by("account_number", "asc");		
//		$by_account_number = $this->db->get();
//		foreach($by_account_number->result() as $row)
//		{
//			$suggestions[]=array('value'=> $row->person_id, 'label' => $row->account_number);		
//		}
//
//		//only return $limit suggestions
//		if(count($suggestions > $limit))
//		{
//			$suggestions = array_slice($suggestions, 0,$limit);
//		}
//		return $suggestions;
//
//	}
//	/*
//	Preform a search on payment
//	*/
	function search($search, $limit=20)
	{
		$this->db->from('payment');
		$this->db->where("(name LIKE '%".$this->db->escape_like_str($search)."%' or 
		id LIKE '%".$this->db->escape_like_str($search)."%'");		
		$this->db->order_by("name", "asc");
		$this->db->limit($limit);
		return $this->db->get();
	}

}
?>
